//
//  LCTaskBaseViewController.m
//  lechuang
//
//  Created by Mr_hi on 2023/3/31.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCTaskBaseViewController.h"

#import "LCTaskAdvertViewController.h"
#import "LCTaskPageViewController.h"

@interface LCTaskBaseViewController ()< UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet LCBaseScrollView *baseScrollView;

@property (weak, nonatomic) IBOutlet UIView *advertBgView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *advertBgViewH;

//任务
@property (weak, nonatomic) IBOutlet UIView *taskContentView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentViewH;

@property (nonatomic, assign) BOOL canScroll;

@end

@implementation LCTaskBaseViewController

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}

-(void)initUI {
    self.canScroll = YES;
    self.baseScrollView.width = ScreenW;
    
    [LCNotiCenter addObserver:self selector:@selector(taskPageControllerLeaveTop:) name:LCTaskPageControllerLeaveTopNoti object:nil];
    
    self.advertBgView.width = ScreenW;
    self.advertBgView.height = 156*proW+21 + 112 + 51;
    self.advertBgViewH.constant = 156*proW+21 + 112 + 51;
    
    LCTaskAdvertViewController * advertVC = [LCTaskAdvertViewController xx_instantiateFromStoryboardNamed:@"Task"];
    advertVC.parentVC = self;
    advertVC.view.frame = CGRectMake(0, 0, ScreenW, 156*proW+21 + 112 + 51);
    [self.advertBgView addSubview:advertVC.view];
    [self addChildViewController:advertVC];
    
    self.taskContentView.width = ScreenW;
    self.taskContentView.height = ScreenH-NavgationHeight-TabBarHeight;
    self.contentViewH.constant = ScreenH-NavgationHeight-TabBarHeight;
    
    LCTaskPageViewController * taskPageVC = [LCTaskPageViewController xx_instantiateFromStoryboardNamed:@"Task"];
    taskPageVC.parentVC = self;
    taskPageVC.view.frame = CGRectMake(0, 0, ScreenW, ScreenH-NavgationHeight-TabBarHeight);
    [self.taskContentView addSubview:taskPageVC.view];
    [self addChildViewController:taskPageVC];
}

- (void)taskPageControllerLeaveTop:(NSNotification *)notif {
    _canScroll = [notif.object boolValue];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat maxOffsetY = 156*proW+21 + 112 + 51;
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY >= maxOffsetY) {
        scrollView.contentOffset = CGPointMake(0, maxOffsetY);
        [[NSNotificationCenter defaultCenter] postNotificationName:LCTaskPageControllerGoTopNoti object:@(YES)];
        _canScroll = NO;
    } else {
        if (!_canScroll) {
            scrollView.contentOffset = CGPointMake(0, maxOffsetY);
        }
    }
}

@end
